/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ControlLayer;
import DBLayer.*;
import ModelLayer.*;

/**
 *
 * @author vildmand
 */
public class CtrlTest {
    
    private CtrlCustomer ctrlCustomer;
    private CtrlProduct ctrlProduct;
    private IFDBSalesOrder dbSalesOrder = new DBSalesOrder();
    private IFDBSalesLineItem dbSalesLineItem = new DBSalesLineItem();
    
    public CtrlTest()
    {
        ctrlCustomer = new CtrlCustomer();
        ctrlProduct = new CtrlProduct();
    }
    
    public void insertNew(SalesOrder saleObj)
      {
           IFDBSalesOrder dbSale = new DBSalesOrder();
           dbSale.insertSalesOrder(saleObj);
      }
    
    public int startSale(String date, String deliveryDate, int customerId)
    {
        
        
        Customer c = ctrlCustomer.findPerson(customerId);
        
        SalesOrder salesOrder = new SalesOrder();
        salesOrder.setDate(date);
        salesOrder.setDeliveryDate(deliveryDate);
        salesOrder.setCustomer(c);
        salesOrder.setDeliveryStatus("false");
        salesOrder.setInvoice(null);
        salesOrder.setTotal(0);
        
        dbSalesOrder.insertSalesOrder(salesOrder);
        
        return dbSalesOrder.findSalesOrder(customerId, false).getOrderId();
    }
    
    public void enterItem(int productId, int amount, int orderId)
    {
        System.out.println("Product id start " + productId);
        Product p = new Product();
        p = ctrlProduct.findProduct(productId);
        System.out.println("Product Id " + p.getProductId());
        SalesOrder sale = dbSalesOrder.findSalesOrder(orderId, false);
        
        SalesLineItem sli = new SalesLineItem();
        sli.setProduct(p);
        sli.setAmount(amount);
        sli.setSalesOrder(sale);
        
        dbSalesLineItem.insertSalesLineItem(sli);
    }
    
    public void endSale(int orderId)
    {
        dbSalesOrder.calcTotalAmounts(dbSalesOrder.findSalesOrder(orderId, false));
    }
    
    public SalesOrder findSalesOrder(int salesOrderId)
    {
        return dbSalesOrder.findSalesOrder(salesOrderId, false);
    }
}
